import {
  RadarOverlay
} from './overlay'
export default {
  data() {
    return {
      center: {
        lng: 116.41338,
        lat: 39.91092
      },
      vertexPoint: null,
      zoom: 15,
      ZOOMBOUNDARY: 14,
      mapInit: null,
      _point: null,
      _title: null,
      _text: null,
      _map: null,
      _div: null,
      _showType: null,
    }
  },
  mounted() {
    this.initMap()
  },
  methods: {
    initMap() {
      let me = this;
      me.mapInit = new BMapGL.Map("groundMap");
      var point = new BMapGL.Point(116.404, 39.915);
      me.mapInit.centerAndZoom(point, me.zoom);
      //开启鼠标滚轮缩放
      me.mapInit.enableScrollWheelZoom(true)
      var scaleCtrl = new BMapGL.ScaleControl(); // 添加比例尺控件
      me.mapInit.addControl(scaleCtrl);
      var zoomCtrl = new BMapGL.ZoomControl(); // 添加缩放控件
      me.mapInit.addControl(zoomCtrl);

      me.mapInit.setMinZoom(8);
      me.mapInit.addEventListener("touchmove", function(e) {
        me.mapInit.enableDragging();
      });
      // TODO: 触摸结束时触发次此事件  此时开启禁止拖动
      me.mapInit.addEventListener("touchend", function(e) {
        me.mapInit.disableDragging();
      });

      //获取当前定位信息
      var geolocation = new BMapGL.Geolocation();
      geolocation.getCurrentPosition(function(r) {
        if (this.getStatus() == BMAP_STATUS_SUCCESS) {
          var mk = new BMapGL.Marker(r.point);
          var point = new BMapGL.Point(r.point.lng, r.point.lat);
          me.mapInit.addOverlay(mk);
          me.mapInit.setCenter(point)
          me.vertexPoint = me.mapInit.getBounds().getSouthWest();
          me.center = me.mapInit.getCenter();
          me.getNearGround()
        }
      });

      //地图拖拽
      me.mapInit.addEventListener('dragend', function(e) {
        me.clearAllOverlays()
        // me.mapInit.clearOverlays()
        me.vertexPoint = me.mapInit.getBounds().getSouthWest();
        me.center = me.mapInit.getCenter();
        if (me.zoom >= me.ZOOMBOUNDARY) {
          me.getNearGround()
        } else {
          me.getStatistics()
        }
      })

      //地图缩放
      me.mapInit.addEventListener('zoomend', function(e) {
        me.clearAllOverlays();
        // me.mapInit.clearOverlays()
        me.zoom = e.target.getZoom();
        me.vertexPoint = e.target.getBounds().getSouthWest();
        me.center = e.target.getCenter();
        if (me.zoom >= me.ZOOMBOUNDARY) {
          me.getNearGround()
        } else {
          me.getStatistics()
        }
      })
    }
  }
}
